<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="交易单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactionNumber">
              <a-input v-model="model.transactionNumber" placeholder="请输入交易单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="交易类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactionType">
              <a-input v-model="model.transactionType" placeholder="请输入交易类型"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="交易对方" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="counterparty">
              <a-input v-model="model.counterparty" placeholder="请输入交易对方"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="商品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="commodity">
              <a-input v-model="model.commodity" placeholder="请输入商品"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="金额(分)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入金额(分)" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="支付账户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentAccount">
              <a-select  v-model="model.paymentAccount" placeholder="请选择支付账户">
                <a-select-option v-for="(item, key) in AccountSelectArrays" :key="key" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-textarea v-model="model.remarks" rows="4" placeholder="请输入备注"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labels">
              <j-multi-select-tag type="checkbox" v-model="model.labels" dictCode="finance_label" placeholder="请选择标签"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="录入方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="importType">
              <j-dict-select-tag type="radio" v-model="model.importType" dictCode="import_type" placeholder="请选择录入方式"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'

export default {
  name: 'FinanceBillForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {xs: {span: 24}, sm: {span: 5},},
      wrapperCol: {xs: {span: 24}, sm: {span: 16},},
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: "/finance/financeBill/add",
        edit: "/finance/financeBill/edit",
        queryById: "/finance/financeBill/queryById",
        AccountSelectList: '/finance/financeAccount/AccountSelectList'
      },
      AccountSelectArrays:[]
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    //
    getAction(this.url.AccountSelectList).then(res => {
      if (res.success) {
        this.AccountSelectArrays = res.result
      }
    });
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      if (record.id == undefined) {
        record.importType = 1
      }
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>